package com.bdqn.Di2Zhan;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * 实战1
 */
public class phoneDisplay {
    public static void main(String[] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try {
            DocumentBuilder builder = factory.newDocumentBuilder();

            Document doc = builder.parse("src/main/java/com/bdqn/Di2Zhan/entity/phone.xml");

            NodeList nodeList = doc.getElementsByTagName("Brand");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node brand = nodeList.item(i);
                Element brandEle = (Element) brand;

                String brandStr = brandEle.getAttribute("name");
                NodeList types = brandEle.getChildNodes();
                for (int j = 0; j < types.getLength(); j++) {
                    Node type = types.item(j);
                    if (type.getNodeType() == Node.ELEMENT_NODE){
                        Element typeEle = (Element) type;
                        String typeStr = typeEle.getAttribute("name");
                        System.out.println(brandStr+"手机："+typeStr);
                    }
                }
            }
        } catch (ParserConfigurationException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (SAXException e) {
            throw new RuntimeException(e);
        }
    }
}
